<?php
class Helper_Download extends Helper
{
	private $_url;
	private $_url_path;
	private $_scheme;
	private $_user;
	private $_pass;
	private $_host;
	private $_port;
	private $_path;
	private $_query;
	private $_httphead;
	private $_head_put;
	
	private $_fp;
	public $error_message;
	
	private function reset()
	{
		$this->_url = '';
		$this->_scheme = 'http';
		$this->_user = '';
		$this->_pass = '';
		$this->_host = '';
		$this->_port = 80;
		$this->_path = '/';
		$this->_query = '';
	}
	
	private function close()
	{
		@fclose($this->_fp);
	}
	
	function url_open( $url, $request_method = 'GET')
	{
		$this->reset();
		$this->close();
		
		$this->_httphead = array();
		
		//解析网址
		if( $this->url_parse($url) == false){
			return false;
		}
		
		//尝试连接主机
		if( $this->host_open($request_method) == false){
			return false;
		}
		
		if( $this->get_head('http-edition') == 'HTTP/1.1') {
			$http_version = 'HTTP/1.1';
		} else {
			$http_version = 'HTTP/1.0';
		}
		
		$header_string = '';
		if($request_method == 'GET') {
			fputs($this->_fp, 'GET '.$this->_url_path.' '.$http_version.'\r\n');
		} else {
			fputs($this->_fp, 'POST '.$this->_path.' '.$http_version.'\r\n');
		}
		
		//发送用户自定义的请求头
		if(!isset($this->_head_put['Accept']) ) {
			$this->_head_put['Accept'] = '*/*';
		}
		if(!isset($this->_head_put['User-Agent']) ) {
			$this->_head_put['Accept'] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)';
		}
		
		if(!isset($this->_head_put['Refer']) ) {
			$this->_head_put['Refer'] = $this->_scheme.'://'.$this->_host;
		}
		
		foreach ($this->_head_put as $k=>$v ){
			if( trim($k) != '' && trim($v) != ''){
				fputs($this->_fp, trim($k).': '.trim($v).'\r\n');
			}
		}
		
		//print_r($header_string);
		//die();
		//fputs($this->_fp, $header_string);
		
		if( $request_method == 'POST') {
			if( !empty($this->_query)) {
				$post_data = $this->_query;
			} else {
				$post_data = 'OK';
			}
			
			$post_data_length = strlen($post_data);
			
			fputs($this->_fp, 'Content-Type: application/x-www-form-urlencoded\r\n');
			fputs($this->_fp, 'Content-Length: '.$post_data_length.'\r\n');
		}
		
		//发送固定的请求结束头
		if( $http_version == 'HTTP/1.1') {
			fputs($this->_fp, 'Connection: Close\r\n\r\n');
		} else {
			fputs($this->_fp, '\r\n');
		}
		
		if($request_method == 'POST'){
			fputs($this->_fp, $post_data);
		}
		
		//获取应答的信息头
		die(print_r(fgets($this->_fp, 256)));
		$http_status = explode(' ', fgets($this->_fp, 256));
		die(var_dump($http_status));
//		$this->_httphead['http_edition'] = trim($http_status[0]);
//		$this->_httphead['http_state'] = trim($http_status[1]);
		
		die('okay');
	}
	
	private function get_head($head_name)
	{
		return isset($this->_httphead[$headname]) ? $this->_httphead[$headname] : '';
	}
	
	//尝试在10秒钟内连接远程主机
	private function host_open()
	{
		$error_no = 0;
		$this->_fp = @fsockopen($this->_host, $this->_port, $error_no, $this->error_message, 10);
		
		if( !$this->_fp ){
			return false;
		}
		
		return true;
	}
	
	private function url_parse($url)
	{
		$url_info = array();
		$url_info = parse_url($url);
		
		if( !is_array($url_info) || empty($url_info['host']) || empty($url_info['scheme']) )
		{
			$this->error_message = '远程网址不正确';
			return false;
		}
		
		$this->_url = $url;
		$this->_host = $url_info['host'];
		$this->_scheme = $url_info['scheme'];
		
		if(!empty($url_info['user'])){
			$this->_user = $url_info['user'];
		}
		
		if(!empty($url_info['pass'])){
			$this->_pass = $url_info['pass'];
		}
		
		if(!empty($url_info['port'])){
			$this->_scheme = $url_info['port'];
		}
		
		if(!empty($url_info['path'])){
			$this->_path = $url_info['path'];
			$this->_url_path = $this->_path;
		}
		
		if(!empty($url_info['query'])){
			$this->_query = $url_info['query'];
			$this->_url_path .= '?'.$this->_query;
		}
		
		return true;
	}
}